const express = require('express');
const router = express.Router();
const dbConfig = require('../db.js')
const mysql = require('mysql');
const sql = require('../sql.js')

const multiparty = require('multiparty');

var connection = mysql.createConnection(dbConfig)
connection.connect();

// 获取全部用户信息
router.use('/allUserInfo', (req, res, next) => {
	connection.query(sql.admin.queryAllUser, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})

// 获取全部车辆信息
router.use('/allCarInfo', (req, res, next) => {
	connection.query(sql.admin.queryAllCar, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})
// 获取用户信息
router.use('/userInfo', (req, res, next) => {
	let u_id = req.query.id;

	connection.query(sql.admin.queryUser, u_id, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})
// 获取车辆信息
router.use('/carInfo', (req, res, next) => {
	let c_id = req.body.id;
	connection.query(sql.admin.queryCar, c_id, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})
// 获取车辆信息
router.use('/getNoUserCarInfo', (req, res, next) => {
	connection.query(sql.admin.getNoUserCarInfo, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})
// 删除用户
router.use('/deleteUser', (req, res, next) => {
	let id = req.body.id;
	connection.query(sql.admin.deleteUser, id, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.send(200);
	})
})
// 删除车辆
router.use('/deleteCar', (req, res, next) => {
	let id = req.body.id;
	connection.query(sql.admin.deleteCar, id, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.sendStatus(200);
	})
})
//增加用户
router.use('/addUser',(req,res,next)=>{
	let u = req.body;
	let r = [u.username,u.password,u.answer]
	connection.query(sql.admin.addUser,r,(err,result,fields)=>{
		if(err){
			console.log(err);
			return res.sendStatus(500)
		}
		res.send(200);
	})
})

// 获取全部车辆行驶信息
router.use('/getAllDriving', (req, res, next) => {
	connection.query(sql.info.getAllDriving, (err, result, fields) => {
		if (err) {
			console.log(err);
			return res.send(500)
		}
		res.status(200).send(result)
	})
})
module.exports = router;
